const Mock = require('mockjs')

const notice = Mock.mock({
  "newsList|35": [{
    id: "@increment",
    title: '@ctitle(5, 10)',
    articeType:"文档",
    content: "@csentence(500)",
    publish_date: "@date(yyyy-MM-dd)",
    'is_comment|1': ['1', '2'],  //是否开启评论 
    author:"@cname()",  //作者
    operator: "@cname()", //修改者
    operate_time: "@date(yyyy-MM-dd hh:mm:ss)",
    'radioPub|1': ['1', '2'],
    visitNum:"@increment(1)",
    // "publishRange|1":['普发',"指定单位","指定员工"],
    publishRange:{},
    ordinaryPub:"1",
    // 记录所有评分数据
    "EvaluateS|2":[{
      importance:"@integer(3,5)",
      content:"@ctitle(5, 10)",
      timestamp:'123237653452'
    }]
  }, ],
  // 单位信息
  "units|30": [{ 
    id: "@increment",
    name:"@cword(2,5)公司",//单位（公司）
    // "departments|5":[{
    //   id: "@increment",
    //   name:["@cword(2,5)部"]
    //   // 'name|1':["@cword(2,5)部","行政办公室","人力资源部","财务部","工会办公室","安全监察部"]
    //   // "departmentName|5":["@cword(2,5)部"]
    // }]
  }, ],
  // 员工信息
  "staffs|30":[{
    empId: "@increment(100000,999999)",
    name:"@cname()", 
    'gender|1': ['0', '1'], //性别
    birth_date:"@date(yyyy-MM-dd)",
    'deptName|1':["行政办公室","人力资源部","财务部","工会办公室","安全监察部","质控管理部","信息技术部","数智企业部"],
    hire_date:"@date(yyyy-MM-dd)",//入职日期
    'is_enable|1': ['是', '否'], 
    address:"@city()@county()",
    'type|1': ['在职', '离职'], //性别
    phone:'13520073575',
    email:'1841031740@qq.com'
  }]
})

module.exports = [
  // 文章数据
  // 通知模块拿数据
  {
    url: '/vue-element-admin/newUsertest/list',
    type: 'post',
    response: config => {
      console.log(config.body)
      // const body = config.body
      const { title, content, author,is_comment,pagenum,pagesize,order} = config.body.query
      var searchDataList = []
      var searchKeyList = []

      if (is_comment === '0') {
        searchDataList = notice.newsList
      } else {
        for (let i = 0; i < notice.newsList.length; i++) {
          if (notice.newsList[i].is_comment === is_comment) {
            searchDataList.push(notice.newsList[i]);
          }
        }
      }
      // 单条件查询（标题）
      // if (title === '') {
      //   searchKeyList = searchDataList
      // } else {
      //   for (let i = 0; i < searchDataList.length; i++) {
      //     if (searchDataList[i].title.indexOf(title) != -1) {
      //       searchKeyList.push(searchDataList[i]);
      //     }
          
      //   }
      // }
      //多条件查询
      searchKeyList = searchDataList.filter(item => {
        let flag = true
        if (title && item.title.indexOf(title) === -1) {
          flag = false
        }
        if (content && item.content.indexOf(content) === -1) {
          flag = false
        }
        if (author && item.author.indexOf(author) === -1) {
          flag = false
        }
        return flag
      })
      if(order==2){
        console.log(order)
        searchKeyList.sort((a, b) => {
          return new Date(b.publish_date) - new Date(a.publish_date);
        });
      }else{
        searchKeyList.sort((a, b) => {
          return new Date(a.publish_date) - new Date(b.publish_date);
        });
      }
      
      // 数据整理
      var [index, size, total] = [pagenum, pagesize, searchKeyList.length]
      var len = total / size
      var totalPages = len - parseInt(len) > 0 ? parseInt(len) + 1 : len
      var newDataList = searchKeyList.slice((index - 1) * size, index * size)
      
      
      return {
        code: 20000,
        data: {
          pagenum: index,
          pagesize: size,
          content: newDataList,
          total: total,
          totalPages: totalPages,
          
        }
      }
    }
  },
  // 获取文章数据预览
  {
    url: '/vue-element-admin/newUsertest/detail',
    type: 'get',
    response: config => {
      const {
        id
      } = config.query
      for (const article of notice.newsList) {
        if (article.id === +id) {
          return {
            code: 20000,
            data: article
          }
        }
      }
    }
  },
  // 通知模块新增数据
  {
    url: '/vue-element-admin/newUsertest/addlist',
    type: 'post',
    response: config => {
      console.log(config.body)
      const body = config.body
      notice.newsList.push(
        Mock.mock({
          id: "@increment",
          title: body.title,
          articeType:body.articeType,
          content: body.content,
          publish_date: body.publish_date,
          is_comment: body.is_comment,
          author: body.author,
          operator: "@cname()",
          operate_time: body.operate_time,
          radioPub: body.radioPub,
          visitNum:"@increment",
          publishRange:body.publishRange,
          ordinaryPub:body.ordinaryPub,
          EvaluateS:""
        })
      )
      return {
        code: 20000,
        msg: "添加成功",
        data: {
          content: notice,
          total: notice.newsList.length,
        }
      }
    }
  },

  // 通知模块删除数据
  {
    url: '/vue-element-admin/newUsertest/dellist',
    type: 'post',
    response: config => {
      console.log(config.body)
      const body = config.body
      const index = notice.newsList.findIndex((item) => {
        return item.id === body.id
      });
      notice.newsList.splice(index, 1)
      console.log(index)
      return {
        code: 20000,
        msg: "删除成功",
        data: {
          content: notice,
          total: notice.newsList.length,
        }
      }
    }
  },
  // 通知模块批量删除数据
  {
    url: '/vue-element-admin/newUsertest/Alldellist',
    type: 'post',
    response: config => {
      let params = config.body.params
      console.log("执行1："+params)
      notice.newsList = notice.newsList.filter(val => !params.includes(val.id))
     
      console.log("notice.newsList",notice.newsList.length)
      // tableList = tableList.filter(val => !deleteArr.includes(val.id))
      return {
        code: 20000,
        msg: "批量删除成功",
        data: {
          content: notice,
          total: notice.newsList.length,
        }
      }
    }
  },

  // 通知模块编辑数据
  {
    url: '/vue-element-admin/newUsertest/editlist',
    type: 'post',
    response: config => {
      console.log(config.body)
      const body = config.body
      var index = notice.newsList.findIndex((item) => {
        return item.id === body.id
      });
      notice.newsList.splice(index, 1, Mock.mock({
        id: body.id,
        title: body.title,
        articeType:body.articeType,
        content: body.content,
        publish_date: body.publish_date,
        is_comment: body.is_comment,
        author: body.author,
        operator: body.operator,
        operate_time: body.operate_time,
        radioPub: body.radioPub,
        visitNum:body.visitNum,
        publishRange:body.publishRange,
        ordinaryPub:body.ordinaryPub
      }))
      console.log(notice.newsList)
      return {
        code: 20000,
        msg: "编辑成功",
        data: {
          content: notice.newsList,
          total: notice.newsList.length,
        }
      }
    }
  },
  // 更新文章浏览数
  {
    url: '/vue-element-admin/newUsertest/ScanCount',
    type: 'post',
    response: config => {
      console.log("sjdhjsdsjed")
      console.log(config.body)
      const body = config.body
      for (var i = 0; i < notice.newsList.length; i++) {
       
        if (notice.newsList[i].id == body.id) {
          notice.newsList[i].visitNum=body.visitNum
          console.log(notice.newsList[i])
        }
      }
      return {
        code: 20000,
        msg: "阅读数增加成功",
      }
    }
  },
  // 新增文章评价数据
  {
    url: '/vue-element-admin/newUsertest/evaluate',
    type: 'post',
    response: config => {
      console.log("sjdhjsdsjed")
      console.log(config.body)
      const {id,form} = config.body
      console.log(notice.newsList.length)
      for (var i = 0; i < notice.newsList.length; i++) {
        if (notice.newsList[i].id == id) {
          notice.newsList[i].EvaluateS.push(form)
        }
      }
      return {
        code: 20000,
        msg: "评价成功",
      }
    }
  },
  // 导出全部数据
  {
    url: '/vue-element-admin/newUsertest/Export',
    type: 'post',
    response: config => {
      console.log(config.body)
      const { title, content, author,is_comment,params} = config.body.query
      var searchDataList = []
      
      if (is_comment === '0') {
        searchDataList = notice.newsList
      } else {
        for (let i = 0; i < notice.newsList.length; i++) {
          if (notice.newsList[i].is_comment === is_comment) {
            searchDataList.push(notice.newsList[i]);
          }
        }
      }
      var ExportDataList = []
      ExportDataList = searchDataList.filter(item => {
        let flag = true
        if (title && item.title.indexOf(title) === -1) {
          flag = false
        }
        if (content && item.content.indexOf(content) === -1) {
          flag = false
        }
        if (author && item.author.indexOf(author) === -1) {
          flag = false
        }
        return flag
      })
      // let params = config.body.params
      console.log("执行1："+params)
      if(params.length>0){
        ExportDataList = ExportDataList.filter(val => params.includes(val.id))
      }
      
      // ExportDataList = notice.newsList
      // var [index, size, total] = [body.pagenum, body.pagesize, searchKeyList.length]
      // var len = total / size
      // var totalPages = len - parseInt(len) > 0 ? parseInt(len) + 1 : len
      // var newDataList = searchKeyList.slice((index - 1) * size, index * size)
      return {
        code: 20000,
        data: {
          // pagenum: index,
          // pagesize: size,
          content: ExportDataList,
          // total: total,
          // totalPages: totalPages,
        }
      }
    }
  },
  // 文章数据 end
  // 单位数据 start
  {
    url: '/vue-element-admin/unitsList/list',
    type: 'post',
    response: config => {
      console.log(config.body)
      const UnitName = config.body.UnitName
      var searchDataList = []
      
      // 单条件查询（标题）
      if (UnitName === '') {
        searchDataList = notice.units
      } else {
        for (let i = 0; i < notice.units.length; i++) {
          if (notice.units[i].label.indexOf(UnitName) != -1) {
            searchDataList.push(notice.units[i]);
          }
          
        }
      }
      return {
        code: 20000,
        data: {
          msg: "数据获取成功",
          unitsList:searchDataList
        }
      }
    }
  },
  // 员工数据 start
  {
    url: '/vue-element-admin/staffsList/list1',
    type: 'post',
    response: config => {
      const { key,currentPage,pageSize} = config.body.params
      var searchDataList = []
      if(key != ""){
        for (let i = 0; i < notice.staffs.length; i++) {
          if (notice.staffs[i].name.indexOf(key) != -1) {
            searchDataList.push(notice.staffs[i]);
          }
        }
      }else{
        searchDataList = notice.staffs
      }
     
      // 数据整理
      var [index, size, total] = [currentPage, pageSize, searchDataList.length]
      var len = total / size
      var totalPages = len - parseInt(len) > 0 ? parseInt(len) + 1 : len
      var newDataList = searchDataList.slice((index - 1) * size, index * size)
      console.log("执行倒了")
      return {
        code: 20000,
        data: {
          msg: "数据获取成功",
          pagenum: index,
          pagesize: size,
          staffList: newDataList,
          total: total,
          totalPages: totalPages,
        }
      }
    }
  },
  // 员工数据 start
  {
    url: '/vue-element-admin/staffsList/list',
    type: 'post',
    response: config => {
      const { key,currentPage,pageSize,form} = config.body.params
      const{empId,name,deptName,hire_date,is_enable}=form
      
      var searchDataList = []
      var ssdata=[]
      // console.log("输出",form)
      
      if(empId!=""||name!=""||deptName!=""||key != ""||hire_date.length>0 || is_enable!= ""){
        if(hire_date!=""){
          for(let i=0;i<notice.staffs.length;i++){
            // &&new Date(ssdata[i].hire_date)<new Date(hire_date[1])
            if(new Date(notice.staffs[i].hire_date)>=new Date(hire_date[0])&&new Date(notice.staffs[i].hire_date)<=new Date(hire_date[1])){
              
              ssdata.push(ssdata[i])
            }
          }
        }else{
          ssdata= notice.staffs
        }
        // searchDataList=ddd
        console.log(ssdata)
        searchDataList = ssdata.filter(item => {
          let flag = true
          item.empId=item.empId.toString()  //员工编号是数字类型将其转换成为字符串类型进行检索
          if (empId && item.empId.indexOf(empId) === -1) {
            flag =false
          }
          if (name && item.name.indexOf(name) === -1) {
            flag = false
          }
          if (deptName && item.deptName.indexOf(deptName) === -1) {
            flag = false
          }
          if (is_enable && item.is_enable.indexOf(is_enable) === -1) {
            flag = false
          }
          return flag
        })
        
        for (let i = 0; i < notice.staffs.length; i++) {
          if (notice.staffs[i].name.indexOf(key) != -1) {
            searchDataList.push(notice.staffs[i]);
          }
        }
      }else{
        searchDataList = notice.staffs
      }
        
      
      
      // 数据整理
      var [index, size, total] = [currentPage, pageSize, searchDataList.length]
      var len = total / size
      var totalPages = len - parseInt(len) > 0 ? parseInt(len) + 1 : len
      var newDataList = searchDataList.slice((index - 1) * size, index * size)
      console.log("执行倒了")
      return {
        code: 20000,
        data: {
          msg: "数据获取成功",
          pagenum: index,
          pagesize: size,
          staffList: newDataList,
          total: total,
          totalPages: totalPages,
        }
      }
    }
  },
  // 获取员工单人数据预览
  {
    url: '/vue-element-admin/staffsList/staffInfo',
    type: 'get',
    response: config => {
      const {
        id
      } = config.query
      console.log(config.query)
      for (const staff of notice.staffs) {
        if (staff.empId === id) {
          return {
            code: 20000,
            data: staff
          }
        }
      }
    }
  },
]
